/*  LocationView.java - Show details about a single location 
 
        Copyright (C) 2009 Devendra Laulkar

        This program is free software: you can redistribute it and/or modify
        it under the terms of the GNU General Public License as published by
        the Free Software Foundation version 2 of the License. 

        This program is distributed in the hope that it will be useful,
        but WITHOUT ANY WARRANTY; without even the implied warranty of
        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
        GNU General Public License for more details.

        You should have received a copy of the GNU General Public License
        along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
package com.intellBatt;

import java.util.Date;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.intellBatt.services.MLService;

public class LocationView extends Activity {

	private static final int MENU_DELETE = 0;
	private static final int MENU_STOP = 1;
	private TextView name, count, lastSeen, comment;
	String origComment;
	LocationDbAdapter dbHelper;
	private Button save;
	public long id;

	public void onCreate(Bundle b)
	{
		super.onCreate(b);
		setContentView(R.layout.location_view);

		name = (TextView)findViewById(R.id.name);
		count = (TextView)findViewById(R.id.Viewcount);
		lastSeen = (TextView)findViewById(R.id.ViewLastSeen);
		comment = (TextView)findViewById(R.id.ViewComment);
		save = (Button)findViewById(R.id.save);

		Bundle extras = getIntent().getExtras();

		if(extras != null)
		{
			id = extras.getLong(MLService.KEY_ID, -1);
			name.setText(extras.getString(MLService.KEY_NAME));
			count.setText("" + extras.getInt(MLService.KEY_COUNT));
			Date d = new Date(extras.getLong(MLService.KEY_LASTSEEN));
			lastSeen.setText(d.toString());
		}

		dbHelper = new LocationDbAdapter(this);	
		dbHelper.open();
		if(id != -1)
		{
			origComment = dbHelper.getComment(id);
			if(origComment == null)
			{
				origComment = "";
			}
			comment.setText(origComment);
		}

		save.setOnClickListener(new View.OnClickListener() {

			public void onClick(View view)
			{
				String comm = comment.getText().toString();
				if(origComment == null )
				{
					if(comm == null || "".equals(comm))
					{
						finish();
					}
					else
					{
						dbHelper.updateComment(id, comment.getText().toString());
						finish();
					}
				}

				else if(!origComment.equals(comm))
				{
					dbHelper.updateComment(id, comm);
				}

				finish();
			}
		});
	}
	public boolean onCreateOptionsMenu(Menu menu) 
	{
		super.onCreateOptionsMenu(menu);
		MenuItem item;
		item = menu.add(0, MENU_DELETE,0, R.string.menuDelete);
		item.setIcon(android.R.drawable.ic_menu_delete);

		item = menu.add(0, MENU_STOP,0, R.string.menuStop);
		item.setIcon(android.R.drawable.ic_menu_close_clear_cancel);

		return true;
	}

	public boolean onMenuItemSelected(int featureId, MenuItem item) 
	{
		super.onMenuItemSelected(featureId, item);
		switch(item.getItemId()) {
		case MENU_DELETE:
			deleteLocation();
			stopService(new Intent("com.IntelBat.services.BATT"));
			stopService(new Intent("com.IntelBat.services.LOCATION"));
			startService(new Intent("com.IntelBat.services.BATT"));
			dbHelper.close();
			finish();
			break;
		case MENU_STOP:
			stopService(new Intent("com.IntelBat.services.BATT"));
			Toast.makeText(this, "Monitoring service stopped",
					Toast.LENGTH_SHORT).show();
			break;
		}

		return true;
	}

	private void deleteLocation() {
		if(id != -1)
		{
			dbHelper.deleteLocation(id);
		}
	}

	public void onDestroy()
	{
		dbHelper.close();
		super.onDestroy();
	}
}
